package com.edu.fafu.studentrepair.fragments;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.edu.fafu.studentrepair.LoginActivity;
import com.edu.fafu.studentrepair.R;
import com.edu.fafu.studentrepair.command.Dictionary;
import com.edu.fafu.studentrepair.command.LocalDataStream;
import com.edu.fafu.studentrepair.command.ReJsonRsp;

import java.io.FileNotFoundException;
import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Human_pwd_change extends Fragment {
    private LinearLayout linearLayout;
    private EditText pwdch_old_pwd;
    private EditText pwdch_new_pwd;
    private EditText pwdch_sure_pwd;
    private Button pwdch_submit;
    private ProgressDialog pd;
    private String pdTitle;
    private String pdMsg;
    private String msg;
    private int clean = 1;
    private int showToast = 2;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == clean)
                clean();
            else if(msg.what == showToast)
                showToast();
             }
    };
    private  void showPD(){
        pd= ProgressDialog.show(getActivity(), pdTitle, pdMsg);
    }
    private  void showPD(String pdTitle,String pdMsg){
        pd= ProgressDialog.show(getActivity(), pdTitle, pdMsg);
    }
    private void showToast(){
        Toast toast = Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG);
        toast.show();
    }
    private void clean(){
        pwdch_old_pwd.setText("");
        pwdch_new_pwd.setText("");
        pwdch_sure_pwd.setText("");
        Toast toast = Toast.makeText(getActivity(),"修改成功",Toast.LENGTH_LONG);
        toast.show();
    }
    private void dismissPD(){
        pd.dismiss();
    }
    @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.human_info_pwd_change,container,false);
        pwdch_old_pwd = view.findViewById(R.id.pwdch_old_pwd);
        pwdch_new_pwd = view.findViewById(R.id.pwdch_new_pwd);
        pwdch_sure_pwd = view.findViewById(R.id.pwdch_sure_pwd);
        pwdch_submit = view.findViewById(R.id.pwdch_submit);
        pwdch_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPD("修改密码","密码修改中");
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //密码
                            String oldpwd = pwdch_old_pwd.getText().toString();
                            String pwd = pwdch_new_pwd.getText().toString();
                            String surePwd = pwdch_sure_pwd.getText().toString();
                            if(!pwd.equals(surePwd)){
                                dismissPD();
                                msg ="两次输入密码不一致";
                                Message message = new Message();
                                message.what=showToast;
                                handler.sendMessage(message);
                                return;
                            }
                            //获得账号
                            String iden = "";
                            try {
                                String readstr = LocalDataStream.read(getActivity().openFileInput("data"));
                                String[] info = readstr.split("_");
                                iden = info[1];
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                            final Request request = new Request.Builder()
                                    .url(Dictionary.url.head+"app/pwdchange?studentId="+iden+"&oldpwd="+oldpwd+"&newpwd="+pwd)
                                    .get()
                                    .build();
                            OkHttpClient okHttpClient = new OkHttpClient();
                            Response response = null;
                            response = okHttpClient.newCall(request).execute();
                            String json = response.body().string();
                            ReJsonRsp reJsonRsp = JSONObject.parseObject(json,new TypeReference<ReJsonRsp>(){});
                            Integer code = reJsonRsp.getCode();
                            Log.i("pwd_change", "run: "+json);
                            if(code.equals(0)){
                                Message message = new Message();
                                message.what=clean;
                                handler.sendMessage(message);
                            }else{
                                msg=reJsonRsp.getMsg();
                                Message message = new Message();
                                message.what=showToast;
                                handler.sendMessage(message);
                            }
                            dismissPD();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                };
                Thread thread = new Thread(runnable);
                thread.start();

            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(hidden)  return;//隐藏页面，不操作
        ((EditText)getActivity().findViewById(R.id.pwdch_old_pwd)).setText("");
        ((EditText)getActivity().findViewById(R.id.pwdch_new_pwd)).setText("");
        ((EditText)getActivity().findViewById(R.id.pwdch_sure_pwd)).setText("");
    }
}
